from typing import Annotated

from fastapi import APIRouter, Cookie, status
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi import Request

from app.controllers.region_controllers import (CreateRegionController,
                                                DeleteRegionController,
                                                EditRegionController,
                                                GetRegionByIdController)
from app.controllers.region_type_controllers import (
    DeleteRegionTypeController, EditRegionTypeController,
    GetRegionTypeByIdController, RegionTypeCreateController)
from app.schemas.region_schemas import (CreateRegionRequestSchema,
                                        CreateRegionResponseSchema,
                                        CreateRegionTypeSchema,
                                        EditRegionTypeSchema,
                                        GetRegionResponseSchema,
                                        GetRegionTypeSchema,
                                        RegionEditRequestSchema,
                                        RegionEditResponseSchema)

region_router = APIRouter(tags=["Region routers"], prefix="/region")
templates = Jinja2Templates(directory="templates")


@region_router.get("", response_class=HTMLResponse)
async def region_front(request: Request):
    return templates.TemplateResponse(
        request=request, name="region.html", context={"id": 1}
    )


@region_router.get("/types", response_class=HTMLResponse)
async def region_type_front(request: Request):
    return templates.TemplateResponse(
        request=request, name="regionType.html", context={"id": 1}
    )


@region_router.post("", status_code=status.HTTP_201_CREATED)
async def create_region(
        request: CreateRegionRequestSchema, id: Annotated[str | None, Cookie()] = None
) -> CreateRegionResponseSchema:
    controller = CreateRegionController(request=request, account_id=int(id))
    return await controller.execute()


@region_router.get("/{regionId}", status_code=status.HTTP_200_OK)
async def get_region(regionId: int) -> GetRegionResponseSchema:
    controller = GetRegionByIdController(region_id=regionId)
    return await controller.execute()


@region_router.put("/{regionId}", status_code=status.HTTP_200_OK)
async def edit_region(
        regionId: int, request: RegionEditRequestSchema
) -> RegionEditResponseSchema:
    controller = EditRegionController(region_id=regionId, request=request)
    return await controller.execute()


@region_router.delete("/{regionId}", status_code=status.HTTP_200_OK)
async def delete_region(regionId: int) -> None:
    controller = DeleteRegionController(region_id=regionId)
    return await controller.execute()


@region_router.post("/types", status_code=status.HTTP_201_CREATED)
async def create_region_type(request: CreateRegionTypeSchema) -> GetRegionTypeSchema:
    controller = RegionTypeCreateController(request=request)
    return await controller.execute()


@region_router.get("/types/{typeId}", status_code=status.HTTP_200_OK)
async def get_region_type(typeId: int) -> GetRegionTypeSchema:
    controller = GetRegionTypeByIdController(type_id=typeId)
    return await controller.execute()


@region_router.put("/types/{typeId}", status_code=status.HTTP_200_OK)
async def edit_region_type(
        typeId: int, request: EditRegionTypeSchema
) -> GetRegionTypeSchema:
    controller = EditRegionTypeController(type_id=typeId, request=request)
    return await controller.execute()


@region_router.delete("/types/{typeId}", status_code=status.HTTP_200_OK)
async def delete_region_type(typeId: int) -> None:
    controller = DeleteRegionTypeController(type_id=typeId)
    return await controller.execute()
